home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / regdat / regdat.frm < prev    next >
Text File  |  1995-05-07  |  6KB  |  154 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "File Association Example"
  4.    ClientHeight    =   1470
  5.    ClientLeft      =   1695
  6.    ClientTop       =   3390
  7.    ClientWidth     =   5430
  8.    Height          =   1875
  9.    Icon            =   0
  10.    Left            =   1635
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   1470
  13.    ScaleWidth      =   5430
  14.    Top             =   3045
  15.    Width           =   5550
  16.    Begin Label Label2 
  17.       Height          =   315
  18.       Left            =   480
  19.       TabIndex        =   1
  20.       Top             =   285
  21.       Width           =   4440
  22.    End
  23.    Begin Label Label1 
  24.       BorderStyle     =   1  'Fixed Single
  25.       Height          =   285
  26.       Left            =   420
  27.       TabIndex        =   0
  28.       Top             =   870
  29.       Width           =   4560
  30.    End
  31. End
  32. DefInt A-Z
  33.  
  34. ' Registration Database (REG.DAT) functions.
  35. ' (Requires Windows 3.1+, or 3.0 with SHELL.DLL.)
  36. '
  37. Declare Function RegCloseKey& Lib "Shell.dll" (ByVal hkey&)
  38. Declare Function RegCreateKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, lphKey&)
  39. Declare Function RegDeleteKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$)
  40. Declare Function RegOpenKey& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, lphKey&)
  41. Declare Function RegQueryValue& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, ByVal lpszValue$, dwLength&)
  42. Declare Function RegSetValue& Lib "Shell.dll" (ByVal hkey&, ByVal lpszSubKey$, ByVal fdwType&, ByVal lpszValue$, ByVal dwLength&)
  43.  
  44. ' Private initialization file (.INI) functions.
  45. '
  46. Declare Function GetPrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpRetString$, ByVal nSize%, ByVal lpFileName$)
  47. Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString As Any, ByVal lpFileName$)
  48.  
  49.  
  50. ''' Written by Lou A. Moccia (thescree@aol.com), 10/94.
  51. ''' Released "as-is" and shared freely with all fellow
  52. ''' VB-WIN programmers.
  53. ''' The REG.DAT code presented here was worked out through
  54. ''' a bit of trial-and-error, because I could not find this
  55. ''' info for VB anywhere, not even that popular "guide to
  56. ''' the Windows API" book for VB.  I do have to give some
  57. ''' credit to the Windows SDK Help file included with VB3,
  58. ''' however shame on Microsoft for only providing dreaded
  59. ''' C examples!
  60. ''' Anyway, I hope you find this code useful!
  61.  
  62. Sub Form_Load ()
  63.  
  64.  
  65. Extension$ = "TXT"
  66.  
  67. Label2.Caption = "Current association for """ & Extension$ & """ files:"
  68.  
  69. Label1.Caption = Get_Association$(Extension$)
  70.  
  71.  
  72. End Sub
  73.  
  74. '--------------------------------------------------------------------------------------
  75. ' Returns the association for the specified file extension. The Registration
  76. ' Database (REG.DAT) file is accessed first by a file utility like File Manager
  77. ' when it attempts to launch a document file, so here REG.DAT is checked for the
  78. ' association first. If nothing is found there, then the [Extensions] section
  79. ' of the WIN.INI file is checked.
  80. '
  81. ' Extension$ = The file extension to check for an association.
  82. '              Sample form:  "txt"
  83. '              Do not include a period before the extension.
  84. '
  85. ' Returns:  The full association string, or an empty string if not found.
  86. '           Sample return from REG.DAT:  "c:\windows\notepad.exe %1"
  87. '           Sample return from WIN.INI:  "c:\windows\notepad.exe ^.txt"
  88. '           It will be up to you to parse off the %1 or ^.xxx part if desired.
  89. '--------------------------------------------------------------------------------------
  90. Function Get_Association$ (Extension$)
  91.  
  92. Get_Association$ = ""
  93. Ext$ = "." & Extension$
  94. tmp$ = Space$(256)
  95. cb& = Len(tmp$)
  96. On Error Resume Next
  97. r& = RegOpenKey(1, Ext$, lphKey&)
  98. If r& = 0& Then
  99.     r1& = RegQueryValue(lphKey&, "shell\open\command", tmp$, cb&)
  100.     ' If no error and the length of the returned string is greater than 1 (a null
  101.     ' is always returned), get the string up to the null and trim it.
  102.     If r1& = 0& And cb& > 1 Then Get_Association$ = Trim$(Left$(tmp$, cb& - 1))
  103.     r1& = RegCloseKey(lphKey&)
  104. Else
  105.     ret% = GetPrivateProfileString("Extensions", Extension$, "", tmp$, Len(tmp$), "WIN.INI")
  106.     ' If the returned string is greater than 5 (the end part ^.xxx),
  107.     ' get the string and trim it.
  108.     If ret% > 5 Then Get_Association$ = Trim$(Left$(tmp$, ret%))
  109. End If
  110.  
  111. End Function
  112.  
  113. '--------------------------------------------------------------------------------------
  114. ' Updates an association in the Registration Database (REG.DAT) file and
  115. ' in the [Extensions] section of the WIN.INI file for the specified file
  116. ' extension, just like File Manager does.
  117. '
  118. ' Extension$ = The file extension to associate or disassociate.
  119. '              Sample form:  "txt"
  120. '              Do not include a period before the extension.
  121. '
  122. ' Application$ = To associate, the application command.
  123. '                Sample form:  "c:\windows\notepad.exe"
  124. '                To disassociate, an empty string.
  125. '
  126. ' Description$ = Optional description of the file extension, used
  127. '                in REG.DAT. Really only useful to yourself.
  128. '                Sample text:  "Text Files"
  129. '                Empty string for no description.
  130. '
  131. ' If does not matter if the association already exists or not.
  132. '--------------------------------------------------------------------------------------
  133. Sub Set_Association (Extension$, Application$, Description$)
  134.  
  135. Ext$ = "." & Extension$
  136. If Len(Application$) = 0 Then
  137.     ret% = WritePrivateProfileString("Extensions", Extension$, 0&, "WIN.INI")
  138.     r& = RegOpenKey(1, "", lphKey&)
  139.     If r& = 0& Then r1& = RegDeleteKey(lphKey&, Ext$)
  140. Else
  141.     Association$ = Application$ & " ^." & Extension$
  142.     ret% = WritePrivateProfileString("Extensions", Extension$, Association$, "WIN.INI")
  143.     r& = RegCreateKey(1, Ext$, lphKey&)
  144.     If r& = 0& Then
  145.         r1& = RegSetValue(lphKey&, "", 1, Description$, 0&)
  146.         Association$ = Application$ & " %1"
  147.         r1& = RegSetValue(lphKey&, "shell\open\command", 1, Association$, 256&)
  148.     End If
  149. End If
  150. If r& = 0& Then r1& = RegCloseKey(lphKey&)
  151.  
  152. End Sub
  153.  
  154.